package service.impl;

import bean.Role;
import dao.MiddleDao;
import dao.RoleDao;
import dao.impl.MiddleDaoImpl;
import dao.impl.RoleDaoImpl;
import service.RoleService;


import java.util.List;

public class RoleServiceImpl implements RoleService {

    private RoleDao dao = new RoleDaoImpl();
    private MiddleDao middleDao = new MiddleDaoImpl();


    @Override
    public List<Role> getRoleList(int pageIndex, Integer pageSize) {
        return dao.getRoleList(pageIndex,pageSize);
    }

    @Override
    public int total() {
        return dao.total();
    }

    @Override
    public List<Role> getList() {
        return dao.getList();
    }

    @Override
    public int insert(String roleName, int roleState, String[] menuIds) {

        //新增角色
        Role role = new Role();
        role.setRoleName(roleName);
        role.setRoleState(roleState);

        int roleId = dao.insert(role);

        //新增中间表
        //插入了多条sql,需要判断数据成功状态
        int k = middleDao.inserMiddle(roleId,menuIds);


        return k;
    }

    @Override
    public Role findById(int roleId) {

        Role role = dao.findById(roleId);

        List menuIds=middleDao.findById(roleId);

        role.setMenuIds(menuIds);

        return role;
    }

    @Override
    public int delete(int roleId) {
        int k = 0;

        int i = middleDao.delete(roleId);

        if (i>0){
            System.out.println("middle中间数据表roleid为："+roleId+"的数据已删除！");
            k = dao.delete(roleId);

        }


        return k;
    }

    @Override
    public int update(Role role) {

        return dao.update(role);
    }
}
